/**
 * Description:该类主要用于创建一个选项卡
 * Copyright: 成都和为科技有限公司 2012
 * Author:zhouxingxing
 * CreateDate:2012-07-18
 * 
 * 属性：
 *   displaynum - 默认显示哪个选项卡, 从0开始计算
 * 
 */
function Tab() {}

Tab.inherit(Container);

/**
 * 初始化方法
 */
Tab.prototype.init = function () {
	Tab.base.init.call(this);
}

Tab.prototype.create = function(){
	this._html = document.createElement("div");
}

Tab.prototype.initLoad = function() {
	Tab.base.initLoad.call(this);
	this.initTab();
}

/**
 * 初始化选项卡
 */
Tab.prototype.initTab = function() {
    var template = "<ul class='nav nav-tabs'></ul><div class='tab-content'></div>";
    $(this.getHTML()).append(template);

	for(var sTabPage in this.getElements()){
		var oTabPage = this.getElements()[sTabPage];
		if (oTabPage.getType() == "TabPage") {
            this.addTabPage(oTabPage);
		}
	}
	var displayNum = 0;
	if(this.getAttribute().displaynum){
		displayNum = this.getAttribute().displaynum;
	}
	this.setDisplayPageNum(displayNum);
}

Tab.prototype.getTabPageCount = function() {
    return $(".nav-tabs li", this.getHTML()).length;
}

/**
 * 获得显示选项卡序列号
 */
Tab.prototype.getDisplayTabNum = function() {
    var num =  $(".nav-tabs li", this.getHTML()).index($(".nav-tabs li.active", this.getHTML()));
    return num;
}

/**
 * 设置选项卡在哪页显示
 */
Tab.prototype.setDisplayPageNum = function(num){
	var that = this;
	$(".nav-tabs li:eq(" + num + ") a", that.getHTML()).tab('show');
}

/**
 * 设置选项卡标题
 */
Tab.prototype.setTabPageTitle = function(num, title) {
    $(".nav-tabs li:eq(" + num + ") a", this.getHTML()).html(title);
}

/**
 * 设置可关闭标题
 */
Tab.prototype.setColseTitle = function(num, title) {
	//$(".nav-tabs li:eq(" + num + ") a", this.getHTML()).html(title + "&nbsp;&nbsp;&nbsp;").append(temp);
	var sTabTitleHTML = $(".nav-tabs li:eq(" + num + ")");
	var othis = this;
	$(".nav-tabs li:eq(" + num + ") a", this.getHTML()).html(title);
	$(".nav-tabs li:eq(" + num + ") a", this.getHTML()).append("&nbsp;&nbsp;&nbsp;" +
	        "<span class='closeicon glyphicon glyphicon-remove' title='移除选项卡'></span>"
	    );
    //关闭按钮点击
    $(".glyphicon-remove", sTabTitleHTML).on("click", function(e) {
        var li = $(this).parent().parent();
        var clickNum = $(".nav-tabs li", othis.getHTML()).index(li);
        othis.removeTabPage(clickNum);
    });
}


/**
 * 获取选项卡选项
 */
Tab.prototype.getTabPageTitle = function(num) {
   return  $(".nav-tabs li:eq(" + num + ") a", this.getHTML()).html();
}

/**
 * 获取选项卡真实的title
 */
Tab.prototype.getTabPageRealTitle = function(num) {
   return  $.trim($(".nav-tabs li:eq(" + num + ") a", this.getHTML()).text());
}
/**
 *  隐藏某一个选项卡
 * @param {} num 序号
 */
Tab.prototype.hideTabPage = function(num) {
    $(".nav-tabs li:eq(" + num + ")", this.getHTML()).hide();
}

/**
 *  显示某一个选项卡
 * @param {} num 序号
 */
Tab.prototype.showTabPage = function(num) {
    $(".nav-tabs li:eq(" + num + ")", this.getHTML()).show();
}

/**
 *  隐藏某一个选项卡
 * @param {} num 序号
 */
Tab.prototype.removeTabPage = function(num) {
	var displayNum = this.getDisplayTabNum();
    if (num == displayNum) { //删除激活的选项卡
        var lastNum = this.getTabPageCount() - 1;
        if (num == lastNum) { //删除最后一个则显示前一个
            displayNum = lastNum - 1;
        } else {
            displayNum = num;
        }
    }
    
    var oTabPage = this.getTabPage(num);
    //回调方法
    var sCloseFun = oTabPage.getAttribute().onclose;
    if (sCloseFun) {
        this.triggerEvent(sCloseFun, oTabPage);
    }
    //删除元素
	var iframes = $("iframe", oTabPage.getHTML());
    if (iframes.length > 0) {
        for (var i = 0 ; i < iframes.length ; i++) {
            this.clearIframe(iframes[i]);
        }
    }
    var sID = $(".nav-tabs li:eq(" + num + ") a", this.getHTML()).attr("href");

    $(".nav-tabs li:eq(" + num + ")", this.getHTML()).remove();
    $(".tab-content " + sID, this.getHTML()).remove();
    this.setDisplayPageNum(displayNum);
}

/**
 * 删除所有选项卡
 */
Tab.prototype.removeAllPage = function() {
    $(".nav-tabs li", this.getHTML()).remove();
    $(".tab-content", this.getHTML()).empty();
}

/**
 * 移除所有可关闭的标签页
 */
Tab.prototype.removeAllClosePage = function() {
    var that = this;
    $(".nav-tabs li", this.getHTML()).each(function(num){
        if ($(".closeicon", this).length > 0) {
            var sID = $("a", this).attr("href");
            $(this).remove();
            $(".tab-content " + sID, that.getHTML()).remove();
        }
    });
    this.setDisplayPageNum(0);
    //$(".tab-content", this.getHTML()).empty();
}

/**
 * 移除所有可关闭未激活标签页
 */
Tab.prototype.removeUnActiveClosePage = function() {
    $(".nav-tabs li", this.getHTML()).each(function(num) {
        if (!$(this).hasClass("active")) {
            if ($(".closeicon", this).length > 0) {
                var sID = $("a", this).attr("href");
                $(this).remove();
                $(".tab-content " + sID, that.getHTML()).remove();
            }
        }
    });
}

/**
 * IE中释放iframe内存
 * @param {} el  注释此代码 ie8 会出现 乱码
 */
Tab.prototype.clearIframe = function (el){
    iframe = el.contentWindow;
    if(el){
        el.src = '';
        try{
            iframe.document.write('');
            iframe.document.clear();
        }catch(e){
//        	el.parentNode.removeChild(iframe); 
        };
    }
}

/**
 * 通过标题判断是否存在tab页
 * @param {} title
 * @return {}
 */
Tab.prototype.hasTabPage = function(title) {
    var found = false;
    $(".nav-tabs li a", this.getHTML()).each(function(index, element) {
        if ($(element).text() == title) {
            found = true;
        }
    });
    return found;
}

/**
 * 通过标题获得tab页序号
 * @param {} title
 * @return {}
 */
Tab.prototype.getTabPageNum = function(title) {
    var num = -1;
    $(".nav-tabs li a", this.getHTML()).each(function(index, element) {
        if ($.trim($(element).text()) == $.trim(title)) {
            num = index;
        }
    });
    return num;
}

 /**
 *  在最后添加一个选项卡
 *  @param {} oTabPage TabPage对象
 *  返回选项卡所在位置,从0开始
 */
Tab.prototype.addTabPage = function(oTabPage) {
    var sTitle = oTabPage.getAttribute().title;
    var sTabPageID = oTabPage.getAttribute().id;
    var sIconClass = oTabPage.getAttribute().iconclass;
    var sCloseable = oTabPage.getAttribute().closeable;
    var sShowFun = oTabPage.getAttribute().onshow;
    var sHideFun = oTabPage.getAttribute().onhide;
    var sHideFunBefore = oTabPage.getAttribute().onhidebefore;
    var sTabTitleHTML = $("<li>" +
        "<a  href='#" + sTabPageID + "' data-toggle='tab' style='cursor:pointer;'>" + sTitle + "</a>" +
    "</li>");

    //增加序号
    sTabTitleHTML[0].tabPage = oTabPage;

    if (sIconClass) {
        $("a", sTabTitleHTML).prepend("<span class='" + sIconClass + "'>&nbsp;</span>");
    }
    var othis = this;
    if (sCloseable == "true") {
        $("a", sTabTitleHTML).append("&nbsp;&nbsp;&nbsp;" +
            "<span class='closeicon glyphicon glyphicon-remove' title='移除选项卡'></span>"
        );
        //关闭按钮点击
        $(".glyphicon-remove", sTabTitleHTML).on("click", function(e) {
            var li = $(this).parent().parent();
            var clickNum = $(".nav-tabs li", othis.getHTML()).index(li);
            othis.removeTabPage(clickNum);
        });
    }
    if (sShowFun) {
        $("a", sTabTitleHTML).attr("show", sShowFun);
        $("a", sTabTitleHTML).on('shown.bs.tab', function (e) {
            var oTabPage = $(this).parent()[0].tabPage;
            var showFun = $(this).attr("show");
            //此处判断是否是main主页上面打开的页面选项卡，如果是就进行当前显示页面的控件显示
            if(typeof(sShowFun)=="boolean" && sShowFun){
            	$("object",$("iframe",oTabPage.getHTML())[0].contentDocument).each(function(){
                	$(this)[0].style.visibility="visible";
                });
            }else{
            	othis.triggerEvent(showFun, oTabPage);
            }
            var tagName = oTabPage.getHTML().firstChild.tagName;
            if(tagName == "IFRAME" || tagName == "iframe" ){
            	if(oTabPage.getHTML().firstChild.outerHTML.indexOf("/public.alarm/comp/alarmlist/AlarmList.html") > 0){
            		othis.triggerEvent("{setAlarmPoint}", oTabPage);
            	}
            }
        });
    }
    if (sHideFun) {
        $("a", sTabTitleHTML).attr("hide", sHideFun);
        $("a", sTabTitleHTML).on('hidden.bs.tab', function (e) {
            var oTabPage = $(this).parent()[0].tabPage;
            var hideFun = $(this).attr("hide");
          //此处判断是否是main主页上面打开的页面选项卡，如果是就进行当前隐藏页面的控件隐藏
            if(typeof(sHideFun)=="boolean" && sHideFun){
            	$("object",$("iframe",oTabPage.getHTML())[0].contentDocument).each(function(){
                	$(this)[0].style.visibility="hidden";
                });
            }else{
            	othis.triggerEvent(hideFun, oTabPage);
            }
            var tagName = oTabPage.getHTML().firstChild.tagName;
            if(tagName == "IFRAME" || tagName == "iframe" ){
            	if(oTabPage.getHTML().firstChild.outerHTML.indexOf("/public.alarm/comp/alarmlist/AlarmList.html") > 0){
            		othis.triggerEvent("{clearAlarm}", oTabPage);
            	}
            }
        });
    }
    if (sHideFunBefore) {
        $("a", sTabTitleHTML).attr("onHideBefore", sHideFunBefore);
        $("a", sTabTitleHTML).on('hide.bs.tab', function (e) {
            var oTabPage = $(this).parent()[0].tabPage;
            var sHideFunBefore = $(this).attr("onHideBefore");
            othis.triggerEvent(sHideFunBefore, oTabPage);
        });
    }
    
    $(this.getHTML()).children(".nav-tabs").append(sTabTitleHTML);
    $(this.getHTML()).children(".tab-content").append(oTabPage.getHTML());
    return this.getTabPageCount() - 1;
}

/**
 *  在最后添加一个选项卡
 *  @param {} oAttr {
 *  	title:'', 
 *  	html:'',     //TabPage里面显示的HTML内容,有bug暂不能使用
 *  	control:'',  //TabPage里面的控件
 *  	iconclass:'', 
 *  	closeable:true, 
 *  	onshow:'{}', 
 *  	onhide:'{}', 
 *  	onclose:'{}'
 *  }
 */
Tab.prototype.addTabPageByAttr = function(oAttr) {
    if (!oAttr.id) {
        oAttr.id = Math.uuid();
    }
    var oTabPage = this.addItem("TabPage", oAttr);
    if (oAttr.control) {
    	oTabPage.appendElement(oAttr.control);
    } else if (oAttr.html) {
    	$(oTabPage.getHTML()).append(oAttr.html);
    }
    return this.addTabPage(oTabPage);
}

Tab.prototype.getTabPage = function(num) {
    return $(".nav-tabs li:eq(" + num + ")", this.getHTML())[0].tabPage;
}